package com.yonyou.gmmc.service.shopping.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.dto.carOwner.CarOwnerDto;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yonyou.gmmc.service.shopping.dao.TmTpAddressMapper;
import com.yonyou.gmmc.service.shopping.dao.TmTpOrderInfoMapper;
import com.yonyou.gmmc.service.shopping.dao.TmTpReceiveAddMapper;
import com.yonyou.gmmc.service.shopping.model.AddressPo;
import com.yonyou.gmmc.service.shopping.model.ReceiveAdd;
import com.yonyou.gmmc.service.shopping.model.TmTpAddress;
import com.yonyou.gmmc.service.shopping.model.TmTpReceiveAdd;

@Transactional
@Service("receiveAddService")
public class ReceiveAddService {
	private Logger logger = Logger.getLogger(ReceiveAddService.class);
	
	@Autowired
	private TmTpReceiveAddMapper tmTpReceiveAddMapper;
	
	@Autowired
	private TmTpAddressMapper tmTpAddressMapper;

    @Autowired
    private TmTpOrderInfoMapper            tmTpOrderInfoMapper;
    
	@Autowired
    private CustomerServiceInterface customerServiceInterface;
	
	/**
     * 获取收货地址
     */
	public List<ReceiveAdd> getReceiveAdd(String openId){
    	List<ReceiveAdd> receiveAddList=new ArrayList<>();
    	receiveAddList=tmTpReceiveAddMapper.queryReceiveAdd(openId);
    	if(receiveAddList.size()==0){
    		//查询默认的姓名电话
    		receiveAddList=tmTpReceiveAddMapper.selectDefaultName(openId);
    		if(receiveAddList.size()==0){
        		receiveAddList.add(new ReceiveAdd());
    		}
    	}
        return receiveAddList;
    }
    
    
    /**
     * 获取所有地址
     */
    public List<TmTpAddress> getAllAddress(){
    	List<TmTpAddress> addressList=tmTpAddressMapper.selectAll();
		return addressList;
    }


    /**
     * 通过parent_code获得地址
     * @param parentCode
     * @return
     */
	public List<TmTpAddress> getAddByCode(String parentCode) {
		Long longPC=null;
		if(!parentCode.equals("null")&&!"".equals(parentCode)){
			longPC=Long.valueOf(parentCode);
		}
		List<TmTpAddress> tmTpAddressList=tmTpAddressMapper.queryAddByCode(longPC);
		return tmTpAddressList;
	}


	/**
	 * 保存收货地址
	 * @param tmTpReceiveAdd
	 * @param openId
	 * @return
	 */
	public Map<String, Object> saveReceiveAdd(TmTpReceiveAdd tmTpReceiveAdd, String openId) {
		
		Map<String, Object> result = new HashMap<String, Object>();
        // 创建日期对象
        Date d = new Date();
        Long defaultAdd=(long) 0;
        
        //根据openId拿到车主
//        List<CarOwnerDto> userDto = customerServiceInterface.getUserInfo(openId);
        String userid = tmTpOrderInfoMapper.queryUserIdByOpenId(openId);
        if(CheckUtil.NullOrEmpty(userid)){
            //非车主
            logger.info("不是车主！");
        }else{
        	//将车主ID存入实体
        	Long userId =new Long(userid);
        	tmTpReceiveAdd.setUserId(userId);
            logger.info("车主ID——"+tmTpReceiveAdd.getUserId());
        	try{
        		//查询该车主是否存在收货地址
        		TmTpReceiveAdd checkReceiveAdd=new TmTpReceiveAdd();
        		checkReceiveAdd=tmTpReceiveAddMapper.selectIfExist(tmTpReceiveAdd);
        		if(checkReceiveAdd==null){
        			//不存在 插入一条
        			tmTpReceiveAdd.setDefaultAdd(defaultAdd);
        			tmTpReceiveAdd.setCreateDate(d);
        			tmTpReceiveAddMapper.insertReceiveAdd(tmTpReceiveAdd);
        			result.put("flag", 1);
                    result.put("msg", "新增成功！");
        		}else{
        			// 存在,更新该记录
        			tmTpReceiveAdd.setUpdateDate(d);
        			tmTpReceiveAddMapper.updateReceiveAdd(tmTpReceiveAdd);
                    result.put("flag", 1);
                    result.put("msg", "更新成功！");
        		}
        	}catch (Exception e) {
                logger.info("Exception:", e);
                result.put("flag", 0);
                result.put("msg", "保存异常！");
            }
        }
        
		return result;
	}
	
	/**
     * 通过parent_code获得地址
     * @param parentCode
     * @return
     */
	public List<AddressPo> selectAllAdd() {
		List<AddressPo> tmTpAddressList=tmTpAddressMapper.selectAllAdd();
		return tmTpAddressList;
	}


	/**
	 * 查询订单详情收货地址
	 * @param thirdOrder
	 * @param openId
	 * @return
	 */
	public List<ReceiveAdd> selectOrderAddress(String thirdOrder,String openId){
		List<ReceiveAdd> receiveAddList=new ArrayList<ReceiveAdd>();
		ReceiveAdd receiveAdd=new ReceiveAdd();
		// 根据OPENID拿到车主ID
//        List<CarOwnerDto> userDto = customerServiceInterface.getUserInfo(openId);
        String userid = tmTpOrderInfoMapper.queryUserIdByOpenId(openId);
        if (CheckUtil.NullOrEmpty(userid)) {
            // 非车主
            logger.info("不是车主！");
            receiveAddList.add(receiveAdd);
        }else{
        	//是车主
        	if(thirdOrder==null||thirdOrder==""){
        		logger.info("thirdOrder为空！");
        		receiveAddList.add(receiveAdd);
        	}else{
        		Long userId =new Long(userid);
            	logger.info("ReceiveAddService.selectOrderAddress.userId....."+userId);
            	receiveAddList=tmTpReceiveAddMapper.selectOrderAddress(thirdOrder,userId);
            	logger.info("tmTpReceiveAddMapper.selectOrderAddress.receiveAddList....."+receiveAddList);
	        	if(receiveAddList.size()==0){
	            	receiveAddList.add(receiveAdd);
	        		logger.info("tmTpReceiveAddMapper.selectOrderAddress.receiveAddList.....为空"+receiveAddList);
	        	}
        	}
        }
        return receiveAddList;
    }
	
}
